package com.example.houtai.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.houtai.config.Logger;
import com.example.houtai.entity.SysLog;
import com.example.houtai.service.ISysLogService;
import com.org.dto.LogDto;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.awt.event.MouseEvent;
import java.lang.reflect.Method;
import java.net.URL;
import java.time.LocalDate;
import java.util.Date;

/**
 * 郝政博
 */
@Component
@Aspect
@Slf4j
public class AopLog {

    //测试

    @Autowired
    private ISysLogService service;

    //切点

    @Pointcut("execution(* com.example.houtai.controller.*.*(..))")
    public void pointcut(){}

    @AfterReturning(pointcut = "pointcut()")
    public void after(JoinPoint joinPoint) {
        //开始时间
        Date startTime=new Date();

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        LogDto logDto=new LogDto();
        //IP地址
        logDto.setIp(request.getRemoteAddr());
        //创建日期
        logDto.setCreateDate(new Date());
        //请求方法
        logDto.setMethod(request.getRequestURI());
        //请求参数
        //logDto.setParams(JSONObject.toJSONString(joinPoint.getArgs()));


        //判断cookie
        String userName ="";
        /*
        Cookie[] ce = request.getCookies();
        if (ce.length>0){
            for(int i=0;i<ce.length;i++) {
                Cookie c = ce[i];
                if(c.toString()!=null) {
                    if(c.getName().equals("token")) {
                        userName = c.getValue();
                    }
                }
            }
        }else{
         userName="暂未登陆";
        }
        */
        userName="暂未登陆";
        logDto.setUsername(userName);
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        String methodName = method.getName();


        if (method.getAnnotation(Logger.class)==null){
            return;
        }

        //自定义注解value
        Logger logger=signature.getMethod().getDeclaredAnnotation(Logger.class);
        String operation=logger.value();
        logDto.setOperation(operation);

        SysLog log=new SysLog();
        BeanUtils.copyProperties(logDto,log);
        Date endTime= new Date();
        int time = (int)(endTime.getTime() - startTime.getTime())/1000;
        //System.out.println("响应方法:"+request.getRequestURI()+"，响应时间:"+time+"请求参数："+JSONObject.toJSONString(joinPoint.getArgs()));
        //响应时间
        log.setTime(Long.parseLong(String.valueOf(time)));
        service.save(log);
    }

}
